package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.util.JSONUtils;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.domain.ShopAdminBean;
import com.dmooo.yxmmerchant.view.contract.ShopPeopleContract;
import com.dmooo.yxmmerchant.view.model.ShopPeopleModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

public class ShopPeoplePreseter extends BasePresenter<ShopPeopleContract.ShopPeopleView> implements ShopPeopleContract.ShopPeoplePtr {
    private ShopPeopleModel model;
    private GsonBuilder builder=new GsonBuilder();
    public ShopPeoplePreseter(ShopPeopleContract.ShopPeopleView mView, Context context) {
        super(mView);
        model=new ShopPeopleModel(context);
        builder.serializeNulls();
    }

    @Override
    public void getGoodsAuth() {
        model.getGoodsAuth(new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                JsonObject object=JSONUtils.getAsJsonObject(o);
                mView.showGoodsAuth(object.get("list").getAsJsonObject());
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());

            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getServiceAuth() {
        model.getServiceAuth(new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                JsonObject object=JSONUtils.getAsJsonObject(o);
                mView.showServiceAuth(object.get("list").getAsJsonObject());
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());

            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getShopAdminList(String token, int page) {
        model.getShopAdminList(token, page, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("dsafds",o.toString());
                try {
                    JSONObject obj=new JSONObject(builder.create().toJson(o));
                    mView.showShopAdminList(new Gson().fromJson(obj.toString(),ShopAdminBean.class).list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void delAdmin(String token, String id, int pos) {
        model.delAdmin(token, id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.delAdminSuccess(pos);
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void addAdmin(String token, String account, String pwd, String job_num, String truename, String contact, String is_freeze, String auth,String order_rate,String service_order_rate) {
        model.addAdmin(token, account, pwd, job_num, truename, contact, is_freeze, auth,order_rate,service_order_rate, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.addAdminSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void editAdmin(String token, String id, String account, String pwd, String job_num, String truename, String contact, String is_freeze, String auth,String order_rate,String service_order_rate) {
        model.editAdmin(token,id, account, pwd, job_num, truename, contact, is_freeze, auth,order_rate,service_order_rate,new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.editSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
